//
//  NetworkManager.m
//  OneStoreFramework
//
//  Created by Aimy on 14-6-29.
//  Copyright (c) 2014年 OneStore. All rights reserved.
//
#import "NetworkManager.h"
#import "NetworkOperationManager.h"
#import <AFNetworking/AFNetworking.h>
#import <CoreCode/CoreCode.h>
@interface NetworkOperationManager()

#pragma mark - Token Expire
/**
 *  功能:登录成功后执行所有暂存的operation
 */
- (void)performCachedOperationsForTokenExpire;

/**
 *  功能:登录失败后清除所有暂存的operation
 */
- (void)clearCachedOperationsForTokenExpire;

#pragma mark - Sign Key Expire
/**
 *  功能:获取密钥接口成功后执行所有暂存的operation
 */
- (void)performCachedOperationsForSignKeyExpire;

/**
 *  功能:获取密钥接口失败后清除所有暂存的operation
 */
- (void)clearCachedOperationsForSignKeyExpire;

#pragma mark - Launch Fail
/**
 *  功能:relaunch成功后执行所有暂存的operation
 */
- (void)performCachedOperationsForLaunchFail;

/**
 *  功能:relaunch失败后清除所有暂存的operation
 */
- (void)clearCachedOperationsForLaunchFail;

#pragma mark - Server Error
/**
 *  功能:服务器错误时将operation暂存
 */
- (void)cacheOperationForServerError:(NetworkOperationParam *)aParam;

/**
 *  功能:服务器错误更换ip后执行所有暂存的operation
 */
- (void)performCachedOperationsForServerError;

/**
 *  功能:更换ip仍然失败后清除所有暂存的operation
 */
- (void)clearCachedOperationsForServerError;

@end

@interface NetworkManager()

@property(nonatomic, strong) NSMutableArray *operationManagers;

@end

@implementation NetworkManager

DEF_SINGLETON(NetworkManager)


- (NSMutableArray *)operationManagers {
    if (_operationManagers == nil) {
        _operationManagers = [NSMutableArray array];
    }
    return _operationManagers;
}

/**
 *  功能:产生一个operation manager
 */
- (NetworkOperationManager *)generateOperationMangerWithOwner:(id)owner {
    NetworkOperationManager *operationManager = [NetworkOperationManager managerWithOwner:owner];
    [self addOperationManger:operationManager];
    
    return operationManager;
}

- (void)addOperationManger:(NetworkOperationManager *)aNetworkOperationManager {
    @synchronized (self.operationManagers) {
        if (aNetworkOperationManager) {
            [self.operationManagers addObject:aNetworkOperationManager];
        }
    }
}

/**
 *  功能:移除operation manager
 */
- (void)removeOperationManger:(NetworkOperationManager *)aNetworkOperationManager {
    @synchronized (self.operationManagers) {
        [self.operationManagers removeObject:aNetworkOperationManager];
    }
}

#pragma mark - Token Expire
/**
 *  功能:登录成功后执行所有暂存的operation
 */
- (void)performAllCachedOperationsForTokenExpire {
    NSArray *copyArray = self.operationManagers.copy;
    for (NetworkOperationManager *manager in copyArray) {
        [manager performCachedOperationsForTokenExpire];
    }
}

/**
 *  功能:登录失败后清除所有暂存的operation
 */
- (void)clearAllCachedOperationsForTokenExpire {
    NSArray *copyArray = self.operationManagers.copy;
    for (NetworkOperationManager *manager in copyArray) {
        [manager clearCachedOperationsForTokenExpire];
    }
}

#pragma mark - Sign Key Expire
/**
 *  功能:获取密钥成功后执行所有暂存的operation
 */
- (void)performAllCachedOperationsForSignKeyExpire {
    NSArray *copyArray = self.operationManagers.copy;
    for (NetworkOperationManager *manager in copyArray) {
        [manager performCachedOperationsForSignKeyExpire];
    }
}

/**
 *  功能:获取密钥失败后清除所有暂存的operation
 */
- (void)clearAllCachedOperationsForSignKeyExpire {
    NSArray *copyArray = self.operationManagers.copy;
    for (NetworkOperationManager *manager in copyArray) {
        [manager clearCachedOperationsForSignKeyExpire];
    }
}

#pragma mark - Launch Fail
/**
 *  功能:执行所有暂存的operation
 */
- (void)performAllCachedOperationsForLaunchFail {
    NSArray *copyArray = self.operationManagers.copy;
    for (NetworkOperationManager *manager in copyArray) {
        [manager performCachedOperationsForLaunchFail];
    }
}

/**
 *  功能:清除所有暂存的operation
 */
- (void)clearAllCachedOperationsForLaunchFail {
    NSArray *copyArray = self.operationManagers.copy;
    for (NetworkOperationManager *manager in copyArray) {
        [manager clearCachedOperationsForLaunchFail];
    }
}

#pragma mark - Server Error
/**
 *  功能:更换ip后执行所有暂存的operation
 */
- (void)performAllCachedOperationsForServerError {
    NSArray *copyArray = self.operationManagers.copy;
    for (NetworkOperationManager *manager in copyArray) {
        [manager performCachedOperationsForServerError];
    }
}

/**
 *  功能:更换ip仍然失败后清除所有暂存的operation
 */
- (void)clearAllCachedOperationsForServerError {
    NSArray *copyArray = self.operationManagers.copy;
    for (NetworkOperationManager *manager in copyArray) {
        [manager clearCachedOperationsForServerError];
    }
}

@end
